package com.asiainfo.wx.handler;

import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.stereotype.Component;

import static me.chanjar.weixin.common.api.WxConsts.EventType;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Slf4j
@Component
public class MenuHandler extends AbstractHandler {

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
        Map<String, Object> context, WxMpService weixinService,
        WxSessionManager sessionManager) {

        String msg = String.format("type:%s, event:%s, key:%s",
            wxMessage.getMsgType(), wxMessage.getEvent(),
            wxMessage.getEventKey());
        if (EventType.VIEW.equals(wxMessage.getEvent())) {
            return null;
        }
        String openId = wxMessage.getOpenId();
        String resultMsg;
        switch (wxMessage.getEventKey()) {
            //余额查询
            case "YECX":
                resultMsg = this.queryBalance(openId);
                break;
            //资源余量查询
            case "YLCX":
                resultMsg = this.queryResourceQuantity(openId);
                break;
            //套餐查询
            case "TCCX":
                resultMsg = this.queryCombo(openId);
                break;
            default:
                resultMsg = "";
        }

        return WxMpXmlOutMessage.TEXT().content(resultMsg)
            .fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser())
            .build();
    }

    /**
     * 查询账户余额信息
     *
     * @param
     * @return
     * @Author: atao
     * @Date: 2019-11-05 17:56
     */
    private String queryBalance(String openId) {
        log.info("调用查询账户余额的服务==start==");
        return "账户余额0元";

    }

    /**
     * 查询套餐组合套餐
     *
     * @param
     * @return
     * @Author: atao
     * @Date: 2019-11-05 17:59
     */
    private String queryCombo(String openId) {
        log.info("调用查询套餐的服务==start==");
        return "套餐为ABC套餐";
    }

    /**
     * 查询资源余量套餐
     *
     * @param
     * @return
     * @Author: atao
     * @Date: 2019-11-05 17:59
     */
    private String queryResourceQuantity(String openId) {
        log.info("调用查询资源余量的服务==start==");
        return "语音剩余10分钟";
    }

}
